package com.sys.controller;

import com.sys.bean.Parklot;
import com.sys.bean.RespBean;
import com.sys.service.ParklotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/admin/parklotController")
public class ParklotController {
    @Autowired
    private ParklotService parklotService;
    //添加停车场
    @PostMapping
    public RespBean insert(@RequestBody Parklot parklot) {
        if (parklotService.selectByParkName(parklot.getParkName())!=null) {
            return RespBean.error("该停车场已存在，添加失败");
        }else{
            return RespBean.ok("添加成功",parklotService.insert(parklot));
        }
    }
    //根据停车场编号删除
    @DeleteMapping("/{parkId}")
    public RespBean delete(@PathVariable("parkId") Integer parkId) {
        if (parklotService.delete(parkId)) {
            return RespBean.ok("删除成功");
        } else {
            return RespBean.error("删除失败，请重新删除！！");
        }
    }
    //修改停车场信息
    @PutMapping
    public RespBean update(@RequestBody Parklot parklot){
        if (parklotService.update(parklot)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    //根据停车场id查询
    @GetMapping("/{parkId}")
    public RespBean selectByParkId(@PathVariable("parkId") Integer parkId) {
        Parklot parklot = parklotService.selectById(parkId);
        return RespBean.ok("", parklot);
    }
    //查询所有停车场
    @GetMapping
    public RespBean selectAll() {
        List<Parklot> parklotList = parklotService.selectAll();
        return RespBean.ok("", parklotList);
    }

}